#
#  untitled.rb
#  
#
#  Created by Zhang Ximin on 8/4/10.
#  Copyright (c) 2010 Columbia University. All rights reserved.
#

require 'watir'
require 'safariwatir'
require 'mysql'
require '/Users/xz2210/Christine_Columbia/Summer_Internship_2010/shermanstravelmedia/environment/html_class_v2/classes/html.class'
require 'resolv'


# Set testing environment
 browser_type = 'Firefox'
# browser_type = 'IE'
#browser_type = 'Safari'
test_environment = 'QA'
# initialize HTML report
@html = HTMLReport.new()
# createReport(reportName, header)
@report = @html.createReport('Shermanstravel_TEST', 'Shermanstravel Media General Test Report', browser_type)

begin 

	#=======================================LUXURY==========================================
	# flag for luxury check
	flag_lux = true
	if(flag_lux)  
	  # new test case 
	  test_case = @html.newTestName('Verify Luxury')
	  # initialization
	  site = "http://www.shermanstravel.com/luxury"
	  #site = "http://widersite.qa.shermanstravel.com/luxury"
	  email_init = 'zhanghotmil@1.com'
	  email_modf = 'zhangxmbxy@hotmail.com'
	  email_lux_valid = false
	  
	  #------------validation check--------
	  test_task = 'luxury page email address validation check'	  
	  load_time = 0
	  # loop begin
	  begin
		  unless email_init.nil?
				unless email_init =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
				# email address is invalid
					@html.add_to_report(false, test_task, 'Warning: email address ('+email_init+') does not appear to be valid; Email is expected to be corrected, the default correction email address is ('+email_modf+') ', 'Warning: email address ('+email_init+') does not appear to be valid; Email is expected to be corrected, the default correction email address is ('+email_modf+')')
				else
				# email address is valid without domain check
				email_lux_valid = true
					if (load_time == 0)
						@html.add_to_report(true, test_task, 'PASS: email address ('+email_init+') runs correctly', 'PASS: email address ('+email_init+') runs correctly') 
					end
				end
		  end	
		  
		  if(email_init.nil?)		
			@html.add_to_report(false, test_task, 'Invalid: email address is blank', 'Invalid: email address is blank')
		  end
		  # test simulation
		  if browser_type == 'IE'
			@browser = Watir::Browser.start(site)
		  elsif browser_type == 'Firefox'
			Watir::Browser.default = 'firefox'
			@browser = Watir::Browser.start(site)
		  else
			Watir::Browser.default = 'safari'
			@browser = Watir::Browser.start(site)
		  end  
		  @browser.text_field(:id, "txtNewsletterSignupEmail").set(email_init)
		  @browser.button(:src,"http://i.stcdn.net/btn_signmeup_light_orange.jpg").click
		  @browser.text_field(:id, "lux_my_zip").set("10025")
		  rescue Exception => e
			if(email_lux_valid)
				puts e.message
				load_time = load_time + 1
				@html.add_to_report(true, test_task, 'Warning: email address is right, for reloading problems, '+load_time+' times has been retried ', 'Warning: email address is right, for reloading problems, '+load_time+' times has been retried')
				retry
			else
				#@html.add_to_report(false, test_task, 'FAIL: email address ('+email_init+') appears to be invalid', 'FAIL: email address ('+email_init+') appears to be invlid') 
				email_init = email_modf
				retry
			end
		  end
		  @browser.checkbox(:id, "si_1").set
		  @browser.checkbox(:id, "si_9").set
		  @browser.text_field(:id, "lux_city").set("ny")
		  @browser.text_field(:id, "lux_addr_two").set("APT 3e")
		  @browser.text_field(:id, "lux_addr_one").set("219 W 100")
		  if browser_type == 'safari'
			@browser.select_list(:id, "people_address_state_or_territory").select("NJ")
			@browser.select_list(:id, "people_address_state_or_territory").select("NY")
			@browser.select_list(:id, "people_address_country_id").select("Tuvalu")
			@browser.select_list(:id, "people_address_country_id").select("United States")
			@browser.text_field(:id, "lux_zip").set("10024")
			@browser.text_field(:id, "lux_phone").set("347-226-1816-1")
			@browser.text_field(:id, "lux_phone").set("347-226-1816")
			@browser.checkbox(:id, "magazine_checkbox").set
			@browser.button(:value,"Submit").click
		  else
			@browser.select_list(:id, "people_address_state_or_territory").set("NJ")
			@browser.select_list(:id, "people_address_state_or_territory").set("NY")
			@browser.select_list(:id, "people_address_country_id").set("Tuvalu")
			@browser.select_list(:id, "people_address_country_id").set("United States")
			@browser.text_field(:id, "lux_zip").set("10024")
			@browser.text_field(:id, "lux_phone").set("347-226-1816-1")
			@browser.text_field(:id, "lux_phone").set("347-226-1816")
			@browser.checkbox(:id, "magazine_checkbox").set
			@browser.button(:value,"Submit").click
		  end
	  end# end for inner begin
	  # ------------new test task--------------
	  test_task = 'Check email info storage'
	  # database		
	  my = Mysql.new("db1.shermanstravel.com", "webuser", "0xQ!=_7R", 'st')
	  res = my.query("SELECT * FROM people where email = '"+email_init+"' " )  
	  # report generation
	  @html.add_to_report(!res.nil?, test_task, 'PASS: Email('+email_init+')  stored successfully', 'FAIL: Unable to store email address '+email_init+' ')

	  #------------postal code check------------
	  # test task info
	  test_task = 'Check postal code storage'
	  if(!res.nil?) # There is information about the email address
		#res.each do |row|
		#	if (!row[0].nil?)
				#res3 = my.query("SELECT * FROM people_addresses pa where pa.id = "+row['address_id'])
				res3 = my.query("SELECT pa.id FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+email_init+"' ")
				res4 = my.query("SELECT pa.postal_code FROM people p, people_addresses pa where pa.id=p.address_id and p.email= '"+email_init+"' ")
				#res3 = my.query("SELECT * FROM people_addresses pa where pa.id = '"+temp_addrid+"' ")
				if(res3) # The user has submitted
					@html.add_to_report(!res4.nil?, test_task, 'PASS: Postal code corresponding to ('+email_init+') has been stored successfully(or exists before) ', 'FAIL: Unable to store postal code with '+email_init+' ')
				else # No submit of the details, no instance
					@html.add_to_report(true, test_task, 'PASS: No instance of ('+email_init+') in people_address table', 'PASS:  No instance of ('+email_init+') in people_address table')
				end
				#  res2 = my.query("SELECT postal_code FROM people p, people_addresses pa where p.email = '"+email_init+"' and p.address_id = pa.id")
			#end
		#end
	  else
		@html.add_to_report(false, test_task, 'Invalid: Email address ('+email_init+') not exists', 'Invalid: Email address ('+email_init+') not exists ')
	  end
	  
	  if browser_type == 'firefox'
	  # Close the current browser
		@browser.close
	  end
	end # begin for if flag	
	
	#=======================================TOP25==========================================
	# top25 flag
	flag_top = true
	if(flag_top)
		# new test case
		test_case = @html.newTestName('Verify Top25')	
		# initialization
		site = "http://shermanstravel.com/top25/signup"
		email = 'zhangggg@hotmail.com'
		if browser_type == 'IE'
		@browser = Watir::Browser.start(site)
		elsif browser_type == 'Firefox'
		Watir::Browser.default = 'firefox'
		@browser = Watir::Browser.start(site)
		else
		Watir::Browser.default = 'safari'
		@browser = Watir::Safari.new
		@browser.goto(site)
		end	

		#--------email validation check------------
		test_task = 'top25 email check'
		# domain check
		email_top_valid = false
		
		def validate_email_domain(email)
		domain = email.match(/\@(.+)/)[1]
		Resolv::DNS.open do |dns|
			@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
			end
		@mx.size > 0 ? true : false
		end
		
		# email address general check
		unless email.blank?
			unless email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
			# email address is invalid
				@html.add_to_report(false, test_task, 'Warning: email address ('+email+') does not appear to be valid', 'Warning: email address ('+email+') does not appear to be valid')
			else
			# email address is valid without domain check
				unless validate_email_domain(email) 
				# domain is invalid
				@html.add_to_report(false, test_task, 'Warning: email domain name appears to be incorrect', 'Warning: email domain name appears to be incorrect') 
				else
				# domain is also valid => email address is valid
				email_top_valid = true
				@html.add_to_report(true, test_task, 'PASS: email address appears to be correct', 'PASS
				: email address appears to be incorrect') 
				end
			end
		end	
		
		if(email.blank?)
			@html.add_to_report(false, test_task, 'Invalid: email address is blank', 'Invalid: email address is blank')
		end
		
		if(email_top_valid)
			# simulation
			@browser.text_field(:id, "primary_email").set(email)
			@browser.text_field(:id, "postal_code").set("10025")
			@browser.button(:src,"http://i.stcdn.net/top25/new_signup/subscribe.jpg").click
			# ---------- Database storage check -------------
			# db connection
			my = Mysql.new("db1.shermanstravel.com", "webuser", "0xQ!=_7R", 'st')
			# email storage check
			test_task = 'check top25 email storage'
			res = my.query("SELECT * FROM people where email = '"+email+"' " )  
			@html.add_to_report(!res.nil?, test_task, 'PASS: Email '+email+' stored successfully or email exists before', 'FAIL: Unable to store email address '+email+'')
			# Close the current browser
			@browser.close
		end
	end
	#--------email storage check---------------
  
  
	#-------zip code storage check-------------
   


	#=======================================NULL CHECK==========================================
	# Note: since there is no real check requested, we also need to make add_to_report not empty
	if(!flag_lux & !flag_top)  
	  test_case = @html.newTestName('NULL CHECK')
	  test_task = 'Empty run check'
	  @html.add_to_report(true, test_task, 'No check requested but works fine', 'No check requested but works fine')
	end



	# General Teardown
	# @browser.close
	@html.finishReport(@report, browser_type, test_environment)
	  
	# General Rescue  
	#rescue => e
	#puts $!
	#puts e.backtrace
	#end
	#@html.finishReport(@report, browser_type, test_environment)
	#~ @browser.close

end

